package linkwechat.controller.Custormer;

import cn.hutool.core.date.DateTime;
import linkwechat.pojo.CustomersSeas;
import linkwechat.pojo.ReturnData;
import linkwechat.service.custormer.ClientService;
import org.apache.poi.ss.usermodel.DataFormatter;
import org.apache.poi.ss.usermodel.Row;
import org.apache.poi.ss.usermodel.Sheet;
import org.apache.poi.ss.usermodel.Workbook;
import org.apache.poi.xssf.usermodel.XSSFWorkbook;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.multipart.MultipartFile;

import java.util.ArrayList;
import java.util.List;

@Controller
@RequestMapping("/Client")
public class ClientController {

    @Autowired
    ClientService clientService;


    //导入客户
    @RequestMapping("/import")
    @ResponseBody
    public ReturnData importExcel(MultipartFile file) throws Exception {
        List<CustomersSeas> userList = new ArrayList<>();
        // 通过当前文件输入流 获取 workbook 工作铺
        Workbook workbook = new XSSFWorkbook(file.getInputStream());
        //获取表 对象
        Sheet sheet = workbook.getSheetAt(0);
        //循环表 的 总行数
        for (int i = 1; i <= sheet.getLastRowNum(); i++) {
            //获取每一行
            Row row = sheet.getRow(i);
            CustomersSeas seas = new CustomersSeas();
            // 设置对象值 通过 获取第一个单元格的数据
            seas.setId((long) row.getCell(0).getNumericCellValue());
            //通过DataDormatter.formatCell 格式化单元格的值为字符串类型
            seas.setPhone(new DataFormatter().formatCellValue(row.getCell(1)));
            seas.setCustomerName(new DataFormatter().formatCellValue(row.getCell(2)));
            seas.setTagNames(new DataFormatter().formatCellValue(row.getCell(3)));
            seas.setTagIds(new DataFormatter().formatCellValue(row.getCell(4)));
            seas.setAddUserName(new DataFormatter().formatCellValue(row.getCell(5)));
            seas.setAddUserId(new DataFormatter().formatCellValue(row.getCell(6)));
            seas.setTableExcelName(file.getName());
            seas.setAddState((int) row.getCell(7).getNumericCellValue());
            seas.setCreateTime(DateTime.now());
            userList.add(seas);
        }
        return clientService.ImportClient(userList);
    }


    //查询客户
    @RequestMapping("/SelectClinet")
    @ResponseBody
    public ReturnData SelectClinet(CustomersSeas customersSeas){
        System.out.println(customersSeas);
        ReturnData returnData = clientService.selectClinet(customersSeas);
        return returnData;
    }

    //批量删除
    @RequestMapping("/DeleteManyClient")
    @ResponseBody
    public ReturnData DeleteManyClient(Long[] ids){
        return clientService.DeleteManyClients(ids);
    }

}
